package controller;

import Pojo.Schedule;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.ScheduleService;

@Controller
@RequestMapping({"/schedule"})
public class ScheduleController
{

  @Autowired
  private ScheduleService scheduleService;

  @RequestMapping({"/selectAll"})
  public String selectAll(Model model, @RequestParam(required=false, defaultValue="1") Integer page, @RequestParam(required=false, defaultValue="5") Integer pageSize)
  {
    PageHelper.startPage(page.intValue(), pageSize.intValue());
    List schedule = this.scheduleService.selectAll();
    PageInfo p = new PageInfo(schedule);
    model.addAttribute("schedule", schedule);
    model.addAttribute("p", p);
    model.addAttribute("mainPage", "schedule.jsp");
    return "admin/main1"; }

  @RequestMapping({"/deleteschedule"})
  public String deleteschedule(Integer id) {
    this.scheduleService.deleteschedule(id);
    return "redirect:selectAll.action"; }

  @RequestMapping({"/insertschedule"})
  public String insertschedule(Schedule schedule, Model model) {
    this.scheduleService.insertschedule(schedule);

    return "redirect:selectAll.action";
  }

  @RequestMapping({"/updateschedule"})
  public String updateschedule(Schedule schedule, Model model) {
    this.scheduleService.updateschedule(schedule);
    model.addAttribute("error", "更新成功");
    model.addAttribute("schedule", schedule);
    model.addAttribute("mainPage", "updateschedule.jsp");
    return "admin/main1";
  }

  @RequestMapping({"/toinsert"})
  public String toinsert(Model model) {
    model.addAttribute("mainPage", "addschedule.jsp");

    return "admin/main1";
  }

  @RequestMapping({"/toupdate"})
  public String toupdate(Model model, Integer id) {
    Schedule schedule = this.scheduleService.selectbyid(id);
    model.addAttribute("schedule", schedule);

    model.addAttribute("mainPage", "updateschedule.jsp");

    return "admin/main1";
  }
}